﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Linq.Dynamic;
using System.Web.Mvc;
using SDT.Model;
using SDT.Lib.Common;
using SDT.Admin.Properties;
using SDT.Admin.ViewModels;
using SDT.Lib.Paging;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.Linq;
using SDT.Lib.Helpers;
using SDT.Systems;

namespace SDT.Admin.Controllers
{
    /// <summary>
    /// Author: Takamaru Nguyen
    /// Date: 24/08/2011
    /// </summary>
    public class MailServerController : Controller
    {
        ChoVietDataContext eDataContext = new ChoVietDataContext();
        //
        // GET: /MailServer/

        public ActionResult Index(int? page)
        {
            try
            {
                if (UserManager.CheckRole(RoleSystem.Mail))
                {
                    if (!page.HasValue)
                        page = 1;
                    string order = UserState.getUserStateFromRequest("mailserver.order", "order", "ID").ToString();
                    string orderDir = UserState.getUserStateFromRequest("mailserver.orderDir", "orderDir", "desc").ToString();
                    string keyword = UserState.getUserStateFromRequest("mailserver.Keyword", "Keyword", "").ToString();
                    int pageSize = int.Parse(UserState.getUserStateFromRequest("mailserver.pageSize", "PageSize", 20).ToString());
                    string strPub = UserState.getUserStateFromRequest("mailserver.CurrentPubStatus", "CurrentPubStatus", "").ToString();

                    int? CurrentPubStatus = null;
                    if (!string.IsNullOrEmpty(strPub))
                        CurrentPubStatus = Convert.ToInt32(strPub);

                    IPagedList<MailServer> mail = eDataContext.MailServers
                        .Where(m => CurrentPubStatus.HasValue ? m.PubYN == (CurrentPubStatus.HasValue && CurrentPubStatus.Value == 1 ? true : false) : true)
                        .Where(m => (string.IsNullOrEmpty(keyword) ? true : m.Name.ToLower().Contains(keyword.ToLower().Trim())))
                        .OrderBy(string.Format("{0} {1}", order, orderDir))
                        .ToPagedList(page.Value - 1, pageSize);

                    ViewData["orderDir"] = orderDir;
                    ViewData["order"] = order;

                    return View(new MailServerViewModels
                    {
                        MailServers = mail,
                        CurrentPubStatus = CurrentPubStatus,
                        Keyword = keyword
                    });
                }
                else
                {
                    ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // GET: /MailServer/Details/5

        public ActionResult Details(int id)
        {
            try
            {
                if (UserManager.CheckRole(RoleSystem.Mail))
                {
                    MailServer mSer = eDataContext.MailServers.Where(ms => ms.ID == id).FirstOrDefault();

                    return View(new MailServerViewModels
                    {
                        MailServer = mSer
                    });
                }
                else
                {
                    ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // GET: /MailServer/Create

        public ActionResult Create()
        {
            try
            {
                if (UserManager.CheckRole(RoleSystem.Mail))
                {
                    return View(new MailServerViewModels());
                }
                else
                {
                    ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        } 

        //
        // POST: /MailServer/Create

        [HttpPost]
        public ActionResult Create(MailServerViewModels m)
        {
            try
            {
                if (UserManager.CheckRole(RoleSystem.Mail))
                {
                    m.MailServer.AddBy = UserManager.GetUserId();
                    m.MailServer.AddDate = DateTime.Now;

                    eDataContext.MailServers.InsertOnSubmit(m.MailServer);
                    eDataContext.SubmitChanges();

                    return RedirectToAction("Index");
                }
                else
                {
                    ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // GET: /MailServer/Edit/5
 
        public ActionResult Edit(int id)
        {
            try
            {
                if (UserManager.CheckRole(RoleSystem.Mail))
                {
                    MailServer mSer = eDataContext.MailServers.Where(ms => ms.ID == id).FirstOrDefault();

                    return View(new MailServerViewModels
                    {
                        MailServer = mSer
                    });
                }
                else
                {
                    ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // POST: /MailServer/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, MailServerViewModels m)
        {
            try
            {
                if (UserManager.CheckRole(RoleSystem.Mail))
                {
                    m.MailServer = eDataContext.MailServers.Where(ms => ms.ID == id).FirstOrDefault();
                    UpdateModel(m);
                    m.MailServer.ModifyBy = UserManager.GetUserId();
                    m.MailServer.ModifyDate = DateTime.Now;

                    eDataContext.SubmitChanges();

                    return RedirectToAction("Index");
                }
                else
                {
                    ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // GET: /MailServer/Delete/5

        public ActionResult Delete(int id)
        {
            try
            {
                if (UserManager.CheckRole(RoleSystem.Mail))
                {
                    MailServer mSer = eDataContext.MailServers.Where(ms => ms.ID == id).FirstOrDefault();
                    return View(mSer);
                }
                else
                {
                    ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // POST: /MailServer/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                if (UserManager.CheckRole(RoleSystem.Mail))
                {
                    MailServer mSer = eDataContext.MailServers.Where(ms => ms.ID == id).FirstOrDefault();
                    eDataContext.MailServers.DeleteOnSubmit(mSer);
                    eDataContext.SubmitChanges();

                    return RedirectToAction("Index");
                }
                else
                {
                    ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
    }
}
